package com.yycx.module.bus.provider.handler.haiying.order;


import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public class AddressHandle {

    public static void main(String[] args) {

        String[] a = AddressHandle.converDetailAddressToProvinceAndCityAndCountyAndStreet("三门峡市湖滨区银昌路2039号");

        System.out.println(a);

        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
//        广东省
//        深圳市
//        福田区
//        新闻路2023号
    }

    /**
     * 处理详细地址拆分省 市 区 地址的转换关系
     *
     * @param detailAddress
     * @return
     */
    public static String[] converDetailAddressToProvinceAndCityAndCountyAndStreet(
        String detailAddress) {
        String[] r = new String[5];
        try {

            String tempStr = detailAddress;
            String province = null;
            int provinceIdx = processProvince(tempStr);
            if (provinceIdx > -1) {
                province = tempStr.substring(0, provinceIdx + 1);
                tempStr = tempStr.substring(provinceIdx + 1);
            }
            String city = null;
            int cityIdx = processCity(tempStr);
            int streetIdx = processStreet(tempStr);
            if (cityIdx > -1) {
                city = tempStr.substring(0, cityIdx + 1);
                tempStr = tempStr.substring(cityIdx + 1);
            }

            String county = null;
            int countyIdx = processCounty(tempStr);
            if (countyIdx > -1 && countyIdx < streetIdx || streetIdx == 0) {
                county = tempStr.substring(0, countyIdx + 1);
                tempStr = tempStr.substring(countyIdx + 1);
            }

            String street = null;
            //与上面的tempStr不同，所以streetIdx不同，上面的streetIdx只做判断使用。
            streetIdx = processStreet(tempStr);
            if (streetIdx > -1) {
                street = tempStr.substring(0, streetIdx + 1);
                tempStr = tempStr.substring(streetIdx + 1);
            }

            String road = tempStr;

            r[0] = province;
            r[1] = city;
            r[2] = county;
            r[3] = street;
            r[4] = road;
        } catch (Exception e) {
            // 报错就直接返回r 为空即可。无法正常转义
            e.printStackTrace();
            return null;
        }

        return r;
    }

    /**
     * 处理详细地址拆分市 区 地址的转换关系
     *
     * @param detailAddress
     * @return
     */
    public static String[] converDetailAddressCityAndCountyAndStreet(
        String detailAddress) {
        String[] r = new String[5];
        try {

            String tempStr = detailAddress;

            String city = null;
            int cityIdx = processCity(tempStr);
            int countyIdx = processCounty(tempStr);
            int streetIdx = processStreet(tempStr);
            if (cityIdx > -1 && cityIdx < countyIdx && cityIdx < streetIdx) {
                city = tempStr.substring(0, cityIdx + 1);
                tempStr = tempStr.substring(cityIdx + 1);
            }

            String county = null;
            countyIdx = processCounty(tempStr);
            streetIdx = processStreet(tempStr);
            //出现的辖区名称在街道后，则为非法辖区值，不会获取
            if (countyIdx > -1 && countyIdx < streetIdx || streetIdx == 0) {
                county = tempStr.substring(0, countyIdx + 1);
                tempStr = tempStr.substring(countyIdx + 1);
            }

            String street = null;
            streetIdx = processStreet(tempStr);//与上面的tempStr不同，所以streetIdx不同，上面的streetIdx只做判断使用。
            if (streetIdx > -1) {
                street = tempStr.substring(0, streetIdx + 1);
                tempStr = tempStr.substring(streetIdx + 1);
            }

            String road = tempStr;
            r[1] = city;
            r[2] = county;
            r[3] = street;
            r[4] = road;
        } catch (Exception e) {
            // 报错就直接返回r 为空即可。无法正常转义
            e.printStackTrace();
            return null;
        }

        return r;
    }

    // (?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)
    private static int processProvince(String s) {
        int[] idxs = new int[3];
        int provinceIdx = -1;
        if ((provinceIdx = s.indexOf("省")) > -1)
            idxs[0] = provinceIdx;
        provinceIdx = -1;
        if ((provinceIdx = s.indexOf("北京市")) > -1 || (provinceIdx = s.indexOf("上海市")) > -1 || (provinceIdx = s.indexOf("天津市")) > -1 ||
            (provinceIdx = s.indexOf("重庆市")) > -1 || (provinceIdx = s.indexOf("自治区")) > -1)
            idxs[1] = provinceIdx + 2;
        provinceIdx = -1;
        if ((provinceIdx = s.indexOf("特别行政区")) > -1)
            idxs[2] = provinceIdx + 4;

        Arrays.sort(idxs);

        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return provinceIdx;
    }

    // (?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)
    private static int processCity(String s) {
        int[] idxs = new int[7];
        int cityIdx = -1;
        if ((cityIdx = s.indexOf("县")) > -1)
            idxs[0] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("自治州")) > -1)
            idxs[1] = cityIdx + 2;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("市辖区")) > -1)
            idxs[2] = cityIdx + 2;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("市")) > -1)
            idxs[3] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("区")) > -1)
            idxs[4] = cityIdx;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("地区")) > -1)
            idxs[5] = cityIdx + 1;
        cityIdx = -1;
        if ((cityIdx = s.indexOf("盟")) > -1)
            idxs[6] = cityIdx;

        Arrays.sort(idxs);

        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return cityIdx;
    }

    // (?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)
    private static int processCounty(String s) {
        int[] idxs = new int[6];
        int countyIdx = -1;
        if ((countyIdx = s.indexOf("县")) > -1)
            idxs[0] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("旗")) > -1)
            idxs[1] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("海域")) > -1)
            idxs[2] = countyIdx + 1;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("市")) > -1)
            idxs[3] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("区")) > -1)
            idxs[4] = countyIdx;
        countyIdx = -1;
        if ((countyIdx = s.indexOf("岛")) > -1)
            idxs[5] = countyIdx;

        Arrays.sort(idxs);

        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return countyIdx;
    }

    /**
     * 获取地址中的街道
     *
     * @param s
     * @return
     */
    private static int processStreet(String s) {
        int[] idxs = new int[4];
        int streetIdx = -1;
        if ((streetIdx = s.indexOf("街道")) > -1)
            idxs[0] = streetIdx + 1;
        streetIdx = -1;
        if ((streetIdx = s.indexOf("镇")) > -1)
            idxs[1] = streetIdx;
        streetIdx = -1;
        if ((streetIdx = s.indexOf("乡")) > -1)
            idxs[2] = streetIdx;
        streetIdx = -1;
        if ((streetIdx = s.indexOf("苏木")) > -1)
            idxs[3] = streetIdx + 1;


        Arrays.sort(idxs);

        for (int i = 0; i < idxs.length; i++) {
            int j = idxs[i];
            if (j > 0) {
                return j;
            }
        }

        return streetIdx;
    }

    /**
     * 获取辖区
     *
     * @param address
     * @return
     */
    public static String getCountry(String address) {
        String xiaqu = null;
        int cityIdx = processCity(address);
        int provinceIdx = processProvince(address);
        int countryIdx = processCounty(address);
        int streetIdx = processStreet(address);

        String[] addrAll;
        if (provinceIdx > -1 && provinceIdx < cityIdx && provinceIdx < countryIdx && provinceIdx < streetIdx) {//判断地址值中有没有省
            addrAll = AddressHandle.converDetailAddressToProvinceAndCityAndCountyAndStreet(address.replace(",", ""));
        } else {
            //传递的地址值可能没有省
            addrAll = AddressHandle.converDetailAddressCityAndCountyAndStreet(address.replace(",", ""));
        }

        if (addrAll != null && CollectionUtil.isNotEmpty(Arrays.asList(addrAll))) {
            for (int i = 0; i < addrAll.length; i++) {
                String addrCity = addrAll[i];
                String addrQu = addrAll[i + 1];
                if (StringUtils.isNoneBlank(addrCity, addrQu) && isCity(addrCity) && isCountry(addrQu)) {
                    xiaqu = addrAll[i + 1];
                    break;
                }
            }
        }
        return xiaqu;
    }

    /**
     * 判断地址是否合法/完整(此处是以地址数组大小 >=3 为标准)
     */
    public static boolean judgeAddrIsLegal(String addr) {
        int cityIdx = processCity(addr);
        int provinceIdx = processProvince(addr);
        int countryIdx = processCounty(addr);
        int streetIdx = processStreet(addr);

        String[] addrAll;
        if (provinceIdx > -1 && provinceIdx < cityIdx && provinceIdx < countryIdx && provinceIdx < streetIdx) {//判断地址值中有没有省
            addrAll = AddressHandle.converDetailAddressToProvinceAndCityAndCountyAndStreet(addr.replace(",", ""));
        } else {
            //传递的地址值可能没有省
            addrAll = AddressHandle.converDetailAddressCityAndCountyAndStreet(addr.replace(",", ""));
        }
        int count = 0;
        if (addrAll != null && CollectionUtil.isNotEmpty(Arrays.asList(addrAll))) {
            for (int i = 0; i < addrAll.length; i++) {
                if (StringUtils.isBlank(addrAll[i])) {
                    ++count;
                }
            }
        } else {
            return false;
        }

        return count <= addrAll.length / 2;
    }

    //判断地址是否为区
    private static boolean isCountry(String s) {
        if (StringUtils.isNotBlank(s)) {
            if (s.contains("小区") || s.contains("社区") || s.contains("居民区")) {
                return false;
            }
            if (s.contains("县")) {
                return true;
            }
            if (s.contains("旗")) {
                return true;
            }
            if (s.contains("海域")) {
                return true;
            }
            if (s.contains("市")) {
                return true;
            }
            if (s.contains("区")) {
                return true;
            }
            if (s.contains("岛")) {
                return true;
            }
        }
        return false;
    }

    //判断地址是否为市
    private static boolean isCity(String s) {
        if (StringUtils.isNotBlank(s)) {
            if (s.contains("县")) {
                return true;
            }
            if (s.contains("自治州")) {
                return true;
            }
            if (s.contains("市辖区")) {
                return true;
            }
            if (s.contains("市")) {
                return true;
            }
            if (s.contains("区")) {
                return true;
            }
            if (s.contains("地区")) {
                return true;
            }
            if (s.contains("盟")) {
                return true;
            }
        }
        return false;
    }
}